package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.xxx.xbutler.data.RobotRoomMapper;
import io.xxx.xbutler.domain.Callback;
import io.xxx.xbutler.domain.RobotRoom;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 机器人退群自动取消关注群回调
 * <p>
 * 当机器人主动退群、被踢出群后，平台会自动取消关注该群，同时该机器人关注群的名额增加一个，自动取消关注状态通过本回调给商家。
 *
 * <pre>
 * {
 *     "nType": 4014,
 *     "vcMerchantNo": "",
 *     "vcRobotWxId": "",
 *     "vcRobotSerialNo": "",
 *     "vcSerialNo": "",
 *     "nResult": 1,
 *     "vcResult": "SUCCESS",
 *     "Data": {
 *         "vcChatRoomSerialNo": "",
 *         "vcChatRoomId": ""
 *     }
 * }
 * </pre>
 */
@Slf4j
@Component
public class _4014Handler implements CallbackHandler {

    @Resource
    private RobotRoomMapper robotRoomMapper;

    @Override
    public String name() {
        return "机器人退群自动取消关注群回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 4014;
    }

    @Override
    public void handle(Callback callback) {
        JSONObject data = (JSONObject) callback.getData();
        LambdaUpdateWrapper<RobotRoom> wrapper = new LambdaUpdateWrapper<RobotRoom>()
                .set(RobotRoom::getOpen, false)
                .eq(RobotRoom::getRobotId, callback.getRobotId())
                .eq(RobotRoom::getRoomId, data.getString("vcChatRoomId"));
        robotRoomMapper.update(wrapper);
    }
}
